<template>
  <div class="container">
    <div class="left">
      <div class="card">
        <el-form ref="loginForm" :model="user" :rules="rules">
          <div>
            <h1 style="font-family: '华文楷体'; font-weight: bold">
              欢迎登录指尖消防
            </h1>
            <div
              style="
                display: flex;
                justify-content: center;
                align-items: center;
                margin: 10px 0 20px 0;
              "
            >
              <span>WELCOME!</span>
            </div>
          </div>
          <el-form-item prop="username">
            <template #default>
              <el-input
                :prefix-icon="User"
                size="large"
                v-model="user.username"
              ></el-input>
            </template>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              :prefix-icon="Lock"
              size="large"
              type="password"
              show-password
              v-model="user.password"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button
              style="width: 100%; margin-top: 20px"
              size="large"
              type="primary"
              color="#626aef"
              @click="submitForm"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>

    <div class="right">
      <el-image
        src="https://s2.loli.net/2024/04/06/OlrPfTc2YghundN.png"
        fit="cover"
        style="height: 100vh"
      ></el-image>
    </div>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { User, Lock } from "@element-plus/icons-vue";
import { doLogin } from "@/api/login";
import router from "@/router";
import { useAuthorizationStore } from "@/stores/authorization";

const authStore = useAuthorizationStore();

const loginForm = ref();
const user = ref({
  username: "admin",
  password: "admin123",
});

const rules = ref({
  username: [{ required: true, message: "请输入用户名", trigger: "blur" }],
  password: [{ required: true, message: "请输入密码", trigger: "blur" }],
});

const submitForm = () => {
  loginForm.value.validate((valid) => {
    if (valid) {
      doLogin(user.value).then((res) => {
        if (res.code === 200) {
          authStore.setAuthorization(res.data);
          router.push("about");
        }
      });
    }
  });
};
</script>

<style scoped>
.container {
  height: 100vh;
  display: flex;
}

.card {
  background-color: #fff;
  box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2);
  height: 340px;
  width: 300px;
  border-radius: 8px;
  padding: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.left {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 50%;
  background-color: #92c1ff;
}

.right {
  width: 50%;
}
</style>
